package com.example.lenovo.weiyi;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.com.ruanmeng.demon.NoticeListM;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.BaseLoadMoreHeaderAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;


public class NotoceListActivity extends BaseActivity {

    @BindView(R.id.rlist_noticelist)
    RecyclerView rlistNoticelist;
    @BindView(R.id.refresh_noticelist)
    SwipeRefreshLayout mRefresh;
    private LinearLayout li_null;
    private LinearLayoutManager linearLayoutManager;
    ArrayList<NoticeListM.RowsBean> Temp_list = new ArrayList<>();
    private int page = 1;
    private HealthListAdapter mAdapter;
    String[] units = {"", "十", "百", "千", "万", "十万", "百万", "千万", "亿",
            "十亿", "百亿", "千亿", "万亿"};
    char[] numArray = {'零', '一', '二', '三', '四', '五', '六', '七', '八', '九'};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notoce_list);
        ButterKnife.bind(this);
        ChangLayTitle("消息");
        init();
        getData();
    }

    public void init() {
        //空
        li_null = (LinearLayout) findViewById(R.id.li_noticelist_null);
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });
        linearLayoutManager = new LinearLayoutManager(this);
        rlistNoticelist.setLayoutManager(linearLayoutManager);
        mAdapter = new HealthListAdapter(NotoceListActivity.this, rlistNoticelist, Temp_list, R.layout.item_notice);
        rlistNoticelist.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent in = new Intent(NotoceListActivity.this, NoticeXQActivity.class);
                in.putExtra("title", Temp_list.get(position + 1).getTitle());
                in.putExtra("time", Temp_list.get(position + 1).getCreateDate());
                in.putExtra("content", Temp_list.get(position + 1).getContent());
                startActivity(in);
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData();
            }

        });
    }

    public void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.NoticeList, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(NotoceListActivity.this, "uid"));

        // 添加到请求队列
        CallServer.getRequestInstance().add(NotoceListActivity.this, 0, mRequest,
                new CustomHttpListener(NotoceListActivity.this, true, NoticeListM.class) {
                    @Override
                    public void doWork(Object data, boolean isOne) {
                        NoticeListM model = ((NoticeListM) data);
                        if (page == 1) {
                            Temp_list.clear();
                        }
                        Temp_list.addAll(model.getRows());
                        if (Temp_list.size() == 0) {
                            li_null.setVisibility(View.VISIBLE);
                            rlistNoticelist.setVisibility(View.GONE);
                        } else {
                            li_null.setVisibility(View.GONE);
                            rlistNoticelist.setVisibility(View.VISIBLE);
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        mRefresh.setRefreshing(false);
                    }
                }, true, true);
    }

    public class HealthListAdapter extends BaseLoadMoreHeaderAdapter<NoticeListM.RowsBean> {


        public HealthListAdapter(Context mContext, RecyclerView recyclerView, List<NoticeListM.RowsBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, RecyclerView.ViewHolder holder, NoticeListM.RowsBean s) {
            View view_ban = (View) holder.itemView.findViewById(R.id.view_text_ban);
            View view_man = (View) holder.itemView.findViewById(R.id.view_text_man);
            TextView tv_text_tilte = (TextView) holder.itemView.findViewById(R.id.tv_notice_tilte);
            TextView time = (TextView) holder.itemView.findViewById(R.id.tv_notice_time);
            time.setText(s.getCreateDate());
            String title = (holder.getLayoutPosition() + 1) + "." +s.getTitle();
            tv_text_tilte.setText(title);
            if (holder.getLayoutPosition() == Temp_list.size() - 1) {
                view_ban.setVisibility(View.GONE);
                view_man.setVisibility(View.VISIBLE);
            } else {
                view_ban.setVisibility(View.VISIBLE);
                view_man.setVisibility(View.GONE);
            }
        }
    }
    /**
     * 数字变汉字
     *
     * @param num
     * @return
     */
    private String foematInteger(int num) {
        char[] val = String.valueOf(num).toCharArray();
        int len = val.length;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            String m = val[i] + "";
            int n = Integer.valueOf(m);
            boolean isZero = n == 0;
            String unit = units[(len - 1) - i];
            if (isZero) {
                if ('0' == val[i - 1]) {
// not need process if the last digital bits is 0
                    continue;
                } else {
// no unit for 0
                    sb.append(numArray[n]);
                }
            } else {
                sb.append(numArray[n]);
                sb.append(unit);
            }
        }
        String sb_str = sb.toString();
        if (sb_str.startsWith("一十")) {
            sb_str = sb_str.substring(1);
        }
        if (sb_str.endsWith("零")) {
            sb_str = sb_str.substring(0, sb_str.length() - 1);
        }
        System.out.println("sb_str==" + sb_str);
        return sb.toString();
    }
}
